/**
 * DemoAction.java
 * @author xsm
 * date 2012-8-8
 */

package com.netistate.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.netistate.base.BaseAction;
import com.netistate.pojo.Demo;
import com.netistate.pojo.DemoHistory;
import com.netistate.pojo.Product;
import com.netistate.pojo.Project;
import com.netistate.service.DemoService;
import com.netistate.service.ProjectService;
import com.netistate.util.GetNowTime;

public class DemoAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private DemoService demoService;
	private ProjectService projectService;
	
	private List<Demo> demoList;
	private List<Product> productList;
	
	//对Demo
	private String id;
	private String model;
	private String seqNo;
	private String license;
	private String demoAddress;
	private String nodes;
	private String module;
	private String hardwareSource;
	private String productConfig;
	private String note;
	private String defaultModule;
	private String customModule;
	private String pid;
	private String orders;
	private String demoSeqNo;
	
	//对产品
	private String projectId;
	private String product;
	private String preVersion;
	private String postVersion;
	private String ip;
	private String mask;
	private String gateway;
	private String dns;
	private String workContent;
	private String workNote;
	private String outDate;
	private String licenseTime;
	private String productNo;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	/**
	 * 添加Demo
	 */
	@Override
	@Action("add_demo")
	public String save() throws Exception {
		
		/*System.out.println("***********");
		System.out.println("pid>>>>>>>>>>>>>："+pid);
		System.out.println("model:"+model);
		System.out.println("seqNo:"+seqNo);
		System.out.println("license:"+license);
		System.out.println("demoAddress:"+demoAddress);
		System.out.println("nodes:"+nodes);
		System.out.println("module:"+module);
		System.out.println("hardwareSource:"+hardwareSource);
		System.out.println("productConfig:"+productConfig);
		System.out.println("note:"+note);
		System.out.println("customModule:"+customModule);
		System.out.println("defaultModule:"+defaultModule);*/
		
		//实查数据库中有多少个demo，去掉重复数
		List<Integer> list = demoService.findAllDemoByOrders();
		
		Demo d = new Demo();
		d.setModel(model);
		d.setSeqNo(seqNo);
		d.setCreatetime(new GetNowTime().getNowTime());
		d.setHardwareSource(hardwareSource);
		d.setProductConfig(productConfig);
		
		//应该把Pid也放进来
		if("".equals(pid)){
			//d.setProjectid(Integer.parseInt(pid));
		}else{
			Project p = projectService.findProjectById(Integer.parseInt(pid));
			d.setProject(p);
		}
		
		d.setOrders(list.size());
		
		d.setLicense(license);
		d.setDemoAddress(demoAddress);
		d.setNote(note);
		d.setNodes(Integer.parseInt(nodes));
		
		//判断模块是否为默认还是定制
		if("".equals(customModule)){
			d.setModule(defaultModule);
		}else{
			d.setModule(customModule);
		}
		
		//默认设置demo的状态为0，即为 在库 状态
		d.setState(0);
		
		demoService.save(d);
		
		return null;
	}

	/**
	 * 删除Demo
	 */
	@Override
	@Action("delete_demo")
	public String del() throws Exception {
		int did = Integer.parseInt(id);
		demoService.del(did);
		return null;
	}
	
	
	/**
	 * 条件查询DEMO
	 * @return
	 * @throws Exception
	 */
	@Action(value="queryDemoByCondition",results={@Result(name="success",type="dispatcher",location="/jsp/demo/demo.jsp")})
	public String queryDemoByCondition() throws Exception{
		
		//实查数据库中有多少个demo，去掉重复数
		List<Integer> list = demoService.findAllDemoByOrders();
		
		List<Demo> dList = new ArrayList<Demo>();
		if("所有".equals(demoSeqNo)){
			dList = demoService.findAllDemo(list);
		}else{
			List<Demo> demoList = demoService.findAllDemo(list);
			for(Demo d : demoList){
				if(d.getSeqNo().equals(demoSeqNo)){
					dList.add(d);
				}
			}
		}
		getRequest().setAttribute("queryDemoSeqNo", demoSeqNo);
		getRequest().setAttribute("dList", dList);
		
		return SUCCESS;
	}
	
	/**
	 * 查询某个Demo的全部信息
	 * @return
	 * @throws Exception
	 */
	@Action("findOneDemo")
	public String findOneDemo() throws Exception{
		
		List<Demo> demoList = demoService.findOneDemoByOrders(orders);
		getSession().put("oneDemoList", demoList);
		
		return null;
	}
	
	/**
	 * 默认查询所有的demo
	 */
	@Override
	@Action("findAllDemo")
	public String findAll() throws Exception {
		List<Integer> list = demoService.findAllDemoByOrders();
		demoList = demoService.findAllDemo(list);
		
		//过滤查询projectList,条件为阶段为售前
		List<Project> SQProjectList = projectService.findProjectByProjectProcess("售前");
		
		getSession().put("SQProjectList", SQProjectList);
		getSession().put("demoList", demoList);
		return null;
	}
	/**
	 * 更新某个demo,不增加记录
	 */
	@Override
	@Action("update_LessDemo")
	public String findById() throws Exception {
		
		System.out.println("id:"+id);
		System.out.println("license:"+license);
		System.out.println("nodes:"+nodes);
		System.out.println("productConfig:"+productConfig);
		System.out.println("note:"+note);
		System.out.println("defaultModule:"+defaultModule);
		System.out.println("customModule:"+customModule);
		
		Demo d = demoService.findDemoById(Integer.parseInt(id));
		d.setLicense(license);
		d.setNodes(Integer.parseInt(nodes));
		d.setProductConfig(productConfig);
		d.setNote(note);
		if("".equals(defaultModule) || defaultModule == null){
			d.setModule(customModule);
		} else {
			d.setModule(defaultModule);
		}
		demoService.save(d);
		return null;
	}

	/**
	 * 点击添加项目服务表时，选择POC,勾选显示相关的demo信息时，就拿pid去找demo
	 * @return
	 * @throws Exception
	 */
	@Action(value="findDemoByPid"/*,results={@Result(name="success",type="dispatcher",location="/jsp/project/addSale.jsp")}*/)
	public String findDemoByPid() throws Exception{
		
		System.out.println("pid:>:>:>:"+pid);
		
		Demo demo = demoService.findDemoByPid(pid); 
		//getRequest().setAttribute("relateDemo", demo);
		
		return null;
	}
	
	/**
	 * 出库DEMO,即是增加一条新记录
	 */
	@Override
	@Action("update_out_demo")
	public String update() throws Exception {
		int did = Integer.parseInt(id);
		Demo demo = demoService.findDemoById(did);
		/*demo.setHardwareSource(hardwareSource);
		demo.setModel(model);
		demo.setNodes(Integer.parseInt(nodes));
		demo.setCreatetime(new GetNowTime().getNowTime());
		demo.setProductConfig(productConfig);
		demo.setSeqNo(seqNo);
		demo.setLicense(license);
		demo.setDemoAddress(demoAddress);
		demo.setNote(note);
		demo.setModule(module);*/
		
		//demoService.update(demo);
		Demo d = new Demo();
		
		//d.setProjectid(Integer.parseInt(pid));
		Project p = projectService.findProjectById(Integer.parseInt(pid));
		d.setProject(p);
		
		d.setOrders(demo.getOrders());
		
		d.setHardwareSource(hardwareSource);
		d.setModel(model);
		d.setNodes(Integer.parseInt(nodes));
		d.setCreatetime(new GetNowTime().getNowTime());
		d.setProductConfig(productConfig);
		d.setSeqNo(seqNo);
		d.setLicense(license);
		d.setDemoAddress(demoAddress);
		d.setNote(note);
		
		//d.setModule(module);
		if("".equals(defaultModule) || defaultModule == null){
			d.setModule(customModule);
		} else {
			d.setModule(defaultModule);
		}
		
		//修改出库/入库信息
		if(demo.getState() == 0){
			d.setState(1);
		}else{
			d.setState(0);
		}
		demoService.save(d);
		
		return null;
	}
	/**
	 * 入库DEMO,即是增加一条新记录
	 */
	@Action("update_in_demo")
	public String update_in_demo() throws Exception {
		int did = Integer.parseInt(id);
		Demo demo = demoService.findDemoById(did);
		
		Demo d = new Demo();
		
		d.setOrders(demo.getOrders());
		d.setHardwareSource(hardwareSource);
		d.setModel(model);
		d.setNodes(Integer.parseInt(nodes));
		d.setCreatetime(new GetNowTime().getNowTime());
		d.setProductConfig(productConfig);
		d.setSeqNo(seqNo);
		d.setLicense(license);
		d.setDemoAddress(demoAddress);
		d.setNote(note);
		
		//d.setProjectid(demo.getProjectid());
		Project p = projectService.findProjectById(demo.getProject().getId());
		d.setProject(p);
		
		//d.setModule(module);
		if("".equals(defaultModule) || defaultModule == null){
			d.setModule(customModule);
		} else {
			d.setModule(defaultModule);
		}
		
		//修改出库/入库信息
		if(demo.getState() == 0){
			d.setState(1);
		}else{
			d.setState(0);
		}
		demoService.save(d);
		
		return null;
	}

	/**
	 * 编辑DEMO
	 */
	@Override
	@Action("edit_demo")
	public String edit() throws Exception {
		int did = Integer.parseInt(id);
		Demo d = demoService.findDemoById(did);
		getSession().put("editDemo", d);
		return null;
	}
	
	/**
	 * 添加产品
	 * @return
	 * @throws Exception
	 */
	@Action("add_product")
	public String addProduct() throws Exception {

		
		Project project = projectService.findProjectById(Integer.parseInt(projectId));
		
		Product p = new Product();
		p.setDns(dns);
		p.setGateway(gateway);
		p.setIp(ip);
		p.setLicenseTime(licenseTime);
		p.setMask(mask);
		//p.setModule(module);		
		p.setPostVersion(postVersion);
		p.setPreVersion(preVersion);
		p.setProduct(product);
		//p.setWorkContent(workContent);
		p.setWorkNote(workNote);		
		p.setOutDate(outDate);		
		p.setNodes(Integer.parseInt(nodes));
		p.setProject(project);
		
		p.setHardwareSource(hardwareSource);
		p.setProductConfig(productConfig);
		
		//判断模块是否为默认还是定制
		if("".equals(customModule)){
			p.setModule(defaultModule);
		}else{
			p.setModule(customModule);
		}
		
		demoService.saveProduct(p);
		return null;
		
	}
	
	/**
	 * 删除产品
	 */
	@Action("delete_product")
	public String delProduct() throws Exception {
		int pid = Integer.parseInt(id);
		demoService.delProduct(pid);
		return null;
	}
	
	/**
	 * 条件查询产品
	 * @return
	 * @throws Exception
	 */
	@Action(value="queryProductByCondition",results={@Result(name="success",type="dispatcher",location="/jsp/demo/product.jsp")})
	public String queryProductByCondition() throws Exception{
		
		List<Product> pdList = demoService.findProductByProductNo(productNo);
		getRequest().setAttribute("queryProductNo", productNo);
		getRequest().setAttribute("pdList", pdList);
		
		return SUCCESS;
	}
	
	/**
	 * 查找全部产品
	 * @return
	 * @throws Exception
	 */
	@Action("findAllProduct")
	public String findAllProduct() throws Exception {
		productList = demoService.findAllProduct();
		getSession().put("productList", productList);
		return null;
	}
	
	/**
	 * 编辑产品
	 * @return
	 */
	@Action("edit_product")
	public String editProduct() throws Exception {
		int pid = Integer.parseInt(id);
		Product p = demoService.findProductById(pid);
		getSession().put("editProduct", p);
		return null;
		
	}
	
	/**
	 * 更新产品,在产品页里更新
	 * @return
	 * @throws Exception
	 */
	@Action("update_product")
	public String updateProduct() throws Exception {
		int pid = Integer.parseInt(id);
		Product pro = demoService.findProductById(pid);
		pro.setDns(dns);
		pro.setGateway(gateway);
		pro.setIp(ip);
		pro.setLicenseTime(licenseTime);
		pro.setMask(mask);
		//pro.setModule(module);
		pro.setNodes(Integer.parseInt(nodes));
		pro.setOutDate(outDate);
		pro.setPostVersion(postVersion);
		pro.setPreVersion(preVersion);
		pro.setProduct(product);
		//pro.setWorkContent(workContent);
		pro.setWorkNote(workNote);
		
		pro.setHardwareSource(hardwareSource);
		pro.setProductConfig(productConfig);
		
		if("".equals(defaultModule) || defaultModule == null){
			pro.setModule(customModule);
		} else {
			pro.setModule(defaultModule);
		}
		
		demoService.updateProduct(pro);
		
		return null;
	}
	
	/**
	 * 在添加服务记录表时对产品的操作
	 * @return
	 * @throws Exception
	 */
	@Action("update_ps_product")
	public String update_ps_product() throws Exception{
		
		int pid = Integer.parseInt(id);
		Product pro = demoService.findProductById(pid);
		pro.setDns(dns);
		pro.setGateway(gateway);
		pro.setIp(ip);
		pro.setProduct(product);
		
		demoService.updateProduct(pro);
		
		return null;
	}
	
	
	//getter & setter
	public List<Demo> getDemoList() {
		return demoList;
	}

	public void setDemoList(List<Demo> demoList) {
		this.demoList = demoList;
	}

	@Autowired
	public void setDemoService(DemoService demoService) {
		this.demoService = demoService;
	}
	
	@Autowired
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}


	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getSeqNo() {
		return seqNo;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setSeqNo(String seqNo) {
		this.seqNo = seqNo;
	}
	public String getLicense() {
		return license;
	}
	public void setLicense(String license) {
		this.license = license;
	}
	public String getDemoAddress() {
		return demoAddress;
	}
	public void setDemoAddress(String demoAddress) {
		this.demoAddress = demoAddress;
	}
	public String getNodes() {
		return nodes;
	}
	public void setNodes(String nodes) {
		this.nodes = nodes;
	}
	public String getModule() {
		return module;
	}
	public void setModule(String module) {
		this.module = module;
	}
	public String getHardwareSource() {
		return hardwareSource;
	}
	public void setHardwareSource(String hardwareSource) {
		this.hardwareSource = hardwareSource;
	}
	public String getProductConfig() {
		return productConfig;
	}
	public void setProductConfig(String productConfig) {
		this.productConfig = productConfig;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getProduct() {
		return product;
	}
	public void setProduct(String product) {
		this.product = product;
	}
	public String getPreVersion() {
		return preVersion;
	}
	public void setPreVersion(String preVersion) {
		this.preVersion = preVersion;
	}
	public String getPostVersion() {
		return postVersion;
	}
	public void setPostVersion(String postVersion) {
		this.postVersion = postVersion;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public String getMask() {
		return mask;
	}
	public void setMask(String mask) {
		this.mask = mask;
	}
	public String getGateway() {
		return gateway;
	}
	public void setGateway(String gateway) {
		this.gateway = gateway;
	}
	public String getDns() {
		return dns;
	}
	public void setDns(String dns) {
		this.dns = dns;
	}
	public String getWorkContent() {
		return workContent;
	}
	public void setWorkContent(String workContent) {
		this.workContent = workContent;
	}
	public String getWorkNote() {
		return workNote;
	}
	public void setWorkNote(String workNote) {
		this.workNote = workNote;
	}
	public String getOutDate() {
		return outDate;
	}
	public void setOutDate(String outDate) {
		this.outDate = outDate;
	}
	public String getLicenseTime() {
		return licenseTime;
	}
	public void setLicenseTime(String licenseTime) {
		this.licenseTime = licenseTime;
	}
	public String getProjectId() {
		return projectId;
	}
	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}
	public String getDefaultModule() {
		return defaultModule;
	}
	public void setDefaultModule(String defaultModule) {
		this.defaultModule = defaultModule;
	}
	public String getCustomModule() {
		return customModule;
	}
	public void setCustomModule(String customModule) {
		this.customModule = customModule;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getOrders() {
		return orders;
	}
	public void setOrders(String orders) {
		this.orders = orders;
	}
	public String getDemoSeqNo() {
		return demoSeqNo;
	}
	public void setDemoSeqNo(String demoSeqNo) {
		this.demoSeqNo = demoSeqNo;
	}
	public String getProductNo() {
		return productNo;
	}
	public void setProductNo(String productNo) {
		this.productNo = productNo;
	}	
	
	
	
}
